package edu.gatech.volt2.drdoommgt.presenter;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import edu.gatech.volt2.drdoommgt.domain.Lab;
import edu.gatech.volt2.drdoommgt.domain.Labwork;
import edu.gatech.volt2.drdoommgt.gui.ILabworkView;
import edu.gatech.volt2.drdoommgt.gui.LabworkView;
import edu.gatech.volt2.drdoommgt.system.DataBaseAccessor;


public class LabworkPresenter implements ActionListener{
	
	private ILabworkView myView;
	private TreatmentRecordPresenter callback = null;
	private DataBaseAccessor dba;

	
	public LabworkPresenter() {
		
		myView = (ILabworkView) new LabworkView();
		myView.addListener(this);
		myView.showSelf();
		
		dba = new DataBaseAccessor();
		dba.connectToDatabase();
		List<Lab> labs = dba.all(Lab.class);
		myView.setLabOptions(labs);
		myView.enableLabSelect();
	}
	public LabworkPresenter(TreatmentRecordPresenter call){
		callback = call;
		myView = (ILabworkView) new LabworkView();
		myView.addListener(this);
		myView.showSelf();
		
		dba = callback.getConnection();
		List<Lab> labs = dba.all(Lab.class);
		myView.setLabOptions(labs);
		myView.enableLabSelect();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if ( e.getSource() == myView.getAddButton() ){
			Labwork lw = new Labwork();
			lw.setDate(myView.getDate());
			Lab liveLab = myView.getLab();
			//liveLab = dba.find(Lab.class, liveLab.getId());
			lw.setLab(liveLab);
			lw.setResults(myView.getResults());
			callback.addNewLabwork(lw);
		}
		myView.hideSelf();
	}

}
